package cn.yudao.org.service.impl;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.BaiduAuditUtils;
import cn.yudao.basic.utils.ValidateUtil;
import cn.yudao.basic.utils.MailUtils;
import cn.yudao.basic.utils.PageList;
import cn.yudao.org.domain.Employee;
import cn.yudao.org.domain.Shop;
import cn.yudao.org.domain.ShopAuditLog;
import cn.yudao.org.mapper.EmployeeMapper;
import cn.yudao.org.mapper.ShopAuditLogMapper;
import cn.yudao.org.mapper.ShopMapper;
import cn.yudao.org.query.ShopQuery;
import cn.yudao.org.service.IEmployeeService;
import cn.yudao.org.service.IShopService;
import cn.yudao.basic.service.impl.BaseServiceImpl;
import cn.yudao.org.vo.ShopVo;
import io.netty.util.collection.ShortObjectMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yudao
 * @since 2022-07-01
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;

    @Autowired
    private IEmployeeService employeeService;

    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();

        // 判断传入的重要信息不能为空
        if (StringUtils.isEmpty(shop.getName()) && StringUtils.isEmpty(shop.getAddress()) ){
            throw new BusinessException("请填写店铺基本信息");
        }
        if (StringUtils.isEmpty(shop.getTel())){
            throw new BusinessException("请填写店铺基本信息");
        }
        // 判断是否输入了管理员值
        if (admin == null){
            throw new BusinessException("请填写管理员基本信息");
        }
        if (StringUtils.isEmpty(admin.getUsername()) && StringUtils.isEmpty(admin.getPhone())){
            throw new BusinessException("请填写管理员基本信息");
        }
        if (StringUtils.isEmpty(admin.getPassword()) && StringUtils.isEmpty(admin.getEmail())){
            throw new BusinessException("请填写管理员基本信息");
        }
        if (StringUtils.isEmpty(admin.getPassword()) && StringUtils.isEmpty(admin.getComfirmPassword())){
            throw new BusinessException("请填写管理员基本信息");
        }
        // 判断邮箱格式是否正确
        if (!ValidateUtil.validateEmail(admin.getEmail())){
            throw new BusinessException("邮箱错误");
        }
        // 判断手机格式是否正确
        if (!ValidateUtil.validatePhone(admin.getPhone())){
            throw new BusinessException("手机号错误");
        }

        // 判断2次密码是否相同
        if (!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("2次输入密码不一致，请重新输入");
        }
        // 判断店铺名称是否违规
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名违规");
        }
        // 判断店铺名称是否违规
        if (!BaiduAuditUtils.TextCensor(shop.getAdmin().getUsername())){
            throw new BusinessException("账号违规");
        }
        // 判断店铺是否被注册使用
        Shop shop1 = shopMapper.loadByAddress(shop.getAddress());
        if (shop1 != null){
            throw new BusinessException("商铺已经被注册");
        }

        // 判断管理员注册，是否绑定了店铺
        Employee employee = employeeMapper.loadPhone(admin.getPhone());
        if (employee == null) {
            employeeService.save(admin);
            employee = admin;
            shop.setAdminId(employee.getId());
            // 判断管理员是否绑定了店铺
        }else if(employee.getShopId() != null) {
            throw new BusinessException("此账号以绑定商品");
        }  // 判断管理员是否被拉黑
        else if(employee.getState() == -1){
            throw new BusinessException("此账号被禁用");
        }
        // 添加店铺
        shopMapper.save(shop);
        // 给管理员的外键赋值
        employee.setShopId(shop.getId());
        // 再次修改员工表
        employeeService.upadte(employee);
    }

    /**
    * @Title: reject
    * @Description: 修改店铺信息邮件
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/3 13:01
    * @Parameters:
    * @Return
    */
    @Override
    public void reject(ShopAuditLog shopAuditLog) {
        Integer shopId = shopAuditLog.getShopId();
        Shop shop = shopMapper.loadById(Long.valueOf(shopId));
        // 设置待激活
        shop.setState(-1);
        // 修改数据库对应表单数据
        shopMapper.upadte(shop);
        // 获取店铺管理员对象
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        // 获取邮箱
        String email = employee.getEmail();
        // 定义一条激活链接
        String content="<h1>亲，因为你的店铺信息存在问题，请点击修改进行对店铺信息修改修改</h1>"+"<a href='http://localhost:8081/#/registerEditor?id=" + shopId + "'>修改</a>";
        // 调用工具类发送邮件
        MailUtils.sendMail(email,content,"修改反馈");
        // 给日志表赋值
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(1);
        shopAuditLogMapper.save(shopAuditLog);
    }

    /**
    * @Title: pass
    * @Description: 激活店铺信息邮件
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/3 13:02
    * @Parameters:
    * @Return
    */
    @Override
    public void pass(ShopAuditLog shopAuditLog) {
        Integer shopId = shopAuditLog.getShopId();
        Shop shop = shopMapper.loadById(Long.valueOf(shopId));
        // 设置待激活
        shop.setState(2);
        // 修改数据库对应表单数据
        shopMapper.upadte(shop);
        // 获取店铺管理员对象
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        // 获取邮箱
        String email = employee.getEmail();
        // 定义一条激活链接
        String content="<h1>亲，请点击激活进行激活店铺操作</h1>"+"<a href='http://localhost:8081/#/login?id=" + shopId + "'>激活</a>";
        // 调用工具类发送邮件
        MailUtils.sendMail(email,content,"亲，激活反馈");
        // 给日志表赋值
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(1);
        shopAuditLogMapper.save(shopAuditLog);
    }
    /**
    * @Title: auditOut
    * @Description: 拒绝店铺邮件
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/3 13:02
    * @Parameters:
    * @Return
    */
    @Override
    public void auditOut(ShopAuditLog shopAuditLog) {
        Shop shop = shopMapper.loadById(Long.valueOf(shopAuditLog.getShopId()));
        shop.setState(-2);
        shopMapper.upadte(shop);
        // 获取店铺管理员对象
        Employee employee = employeeMapper.loadById(shop.getAdminId());
        // 获取邮箱
        String email = employee.getEmail();
        // 定义一条激活链接
        String content="<h1>因为你的不规范行为所以导致你的店铺被拒绝在本平台上使用</h1>";
        // 调用工具类发送邮件
        MailUtils.sendMail(email,content,"拒绝反馈");
        // 给日志表赋值
        shopAuditLog.setAuditTime(new Date());
        shopAuditLog.setAuditId(1);
        shopAuditLogMapper.save(shopAuditLog);

    }

    @Override
    public Shop loadBylogininfoId(Long id) {

        return shopMapper.loadBylogininfoId(id);
    }

    /**
    * @Title: activation
    * @Description: 根据id激活当前店铺
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/2 19:49
    * @Parameters:
    * @Return
    */
    @Override
    public void activation(Long id) {
        Shop shop = shopMapper.loadById(id);
        shop.setState(3);
        shopMapper.upadte(shop);
    }

    @Override
    public void loadSave(List<Shop> shops) {
        shopMapper.loadSave(shops);
    }

    @Override
    public Map<String,List> loadEcharts() {
        List<ShopVo> shopVos = shopMapper.loadEcharts();
        HashMap<String, List> stringListHashMap = new HashMap<>();
        List<String> x = new ArrayList<String>();
        List<Integer> y = new ArrayList<Integer>();
        //店铺状态：0待审核，1以审核，-1驳回，2待激活
        for (ShopVo shopVo : shopVos) {
            if (shopVo.getState() == 0){
                x.add("待审核");
            }else if(shopVo.getState() == 1){
                x.add("以审核");
            }else if(shopVo.getState() == 2){
                x.add("待激活");
            }else if(shopVo.getState() == -1){
                x.add("驳回");
            }else if(shopVo.getState() == -2){
                x.add("拒绝");
            }else if(shopVo.getState() == 3){
                x.add("激活");
            }
            y.add(shopVo.getCount());
        }

        stringListHashMap.put("x",x);
        stringListHashMap.put("y",y);

        return stringListHashMap;
    }

    @Override
    public PageList<Shop> query(ShopQuery shopQuery) {
        // 查询当前页数据
        List<Shop> rows = shopMapper.query(shopQuery);
        // 查询当前页条数
        Integer total = shopMapper.pagecount(shopQuery);

        // 查询日志
        List<ShopAuditLog> shopAuditLogs = shopAuditLogMapper.LoadAll();

        shopAuditLogs.forEach(System.out::println);
        for (Shop row : rows) {
            for (ShopAuditLog shopAuditLog : shopAuditLogs) {
                if(Long.valueOf(shopAuditLog.getShopId()) == row.getId()){
                    row.getList().add(shopAuditLog);
                    shopAuditLog.getShop().setState(row.getState());
                }
            }
        }
        return new PageList<Shop>(total, rows);
    }

    /**
    * @Title: upadteChange
    * @Description: 驳回后修改
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/3 11:27
    * @Parameters:
    * @Return
    */
    @Override
    public void upadteChange(Shop shop) {
        Shop shop1 = shopMapper.loadById(shop.getId());
        // 判断传入的重要信息不能为空
        if (StringUtils.isEmpty(shop.getName()) && StringUtils.isEmpty(shop.getAddress()) && StringUtils.isEmpty(shop.getTel()) ){
            throw new BusinessException("请填写店铺基本信息");
        }
        // 判断店铺名称是否违规
        if (!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("店铺名违规");
        }
        shop1.setState(0);
        shop1.setName(shop.getName());
        shop1.setAddress(shop.getAddress());
        shop1.setTel(shop.getTel());
        shop1.setLogo(shop.getLogo());

        shopMapper.upadte(shop1);

    }




}